package liuds.music;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;

import liuds.music.model.RequestParams;
import liuds.music.model.User;
import liuds.music.utils.Http;

/**
 * Created by Administrator on 2018/6/22.
 */

public class LoginActivity extends BaseActivity {

    private EditText username,password;
    private Button loginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        toolbar.setNavigationIcon(null);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        loginBtn = findViewById(R.id.loginBtn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams params = new RequestParams();
                params.put("username",username.getText());
                params.put("password",password.getText());
                Http.post("/user/login", params, new Http.OnSuccessListener() {
                    @Override
                    public void onSuccess(Object result) {
                        App.USER = new Gson().fromJson(result.toString(), User.class);
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                },null,null);
            }
        });
    }
}
